package com.ruoyi.web.controller.university;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UniversityMajor;
import com.ruoyi.system.service.IUniversityMajorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大学专业关联Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/university/major")
public class UniversityMajorController extends BaseController
{
    @Autowired
    private IUniversityMajorService universityMajorService;

    /**
     * 查询大学专业关联列表
     */
    @PreAuthorize("@ss.hasPermi('university:major:list')")
    @GetMapping("/list")
    public TableDataInfo list(UniversityMajor universityMajor)
    {
        startPage();
        List<UniversityMajor> list = universityMajorService.selectUniversityMajorList(universityMajor);
        return getDataTable(list);
    }

    /**
     * 导出大学专业关联列表
     */
    @PreAuthorize("@ss.hasPermi('university:major:export')")
    @Log(title = "大学专业关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UniversityMajor universityMajor)
    {
        List<UniversityMajor> list = universityMajorService.selectUniversityMajorList(universityMajor);
        ExcelUtil<UniversityMajor> util = new ExcelUtil<UniversityMajor>(UniversityMajor.class);
        util.exportExcel(list, "大学专业关联数据");
    }

    /**
     * 获取大学专业关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('university:major:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(universityMajorService.selectUniversityMajorById(id));
    }

    /**
     * 获取大学相关专业列表
     */
    @PreAuthorize("@ss.hasPermi('university:major:query')")
    @GetMapping(value = "/university/{universityId}")
    public AjaxResult getMajorsByUniversity(@PathVariable("universityId") Long universityId)
    {
        return success(universityMajorService.selectMajorsByUniversityId(universityId));
    }

    /**
     * 获取专业相关大学列表
     */
    @PreAuthorize("@ss.hasPermi('university:major:query')")
    @GetMapping(value = "/major/{majorId}")
    public AjaxResult getUniversitiesByMajor(@PathVariable("majorId") Long majorId)
    {
        return success(universityMajorService.selectUniversitiesByMajorId(majorId));
    }

    /**
     * 新增大学专业关联
     */
    @PreAuthorize("@ss.hasPermi('university:major:add')")
    @Log(title = "大学专业关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UniversityMajor universityMajor)
    {
        if (!universityMajorService.checkUniversityMajorUnique(universityMajor))
        {
            return error("新增大学专业关联'" + universityMajor.getUniversityName() + "'失败，该关联已存在");
        }
        return toAjax(universityMajorService.insertUniversityMajor(universityMajor));
    }

    /**
     * 修改大学专业关联
     */
    @PreAuthorize("@ss.hasPermi('university:major:edit')")
    @Log(title = "大学专业关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UniversityMajor universityMajor)
    {
        if (!universityMajorService.checkUniversityMajorUnique(universityMajor))
        {
            return error("修改大学专业关联'" + universityMajor.getUniversityName() + "'失败，该关联已存在");
        }
        return toAjax(universityMajorService.updateUniversityMajor(universityMajor));
    }

    /**
     * 删除大学专业关联
     */
    @PreAuthorize("@ss.hasPermi('university:major:remove')")
    @Log(title = "大学专业关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(universityMajorService.deleteUniversityMajorByIds(ids));
    }
}
